<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script>
        let socket;
        if (window.WebSocket) {
            socket = new WebSocket("ws://localhost:8080/hello");
            socket.onmessage = function (ev) {
                let rt = document.getElementById("responseText");
                rt.value = rt.value + "\n" + ev.data;
            }
            socket.onopen = function (ev) {
                let rt = document.getElementById("responseText");
                rt.value = "连接开启了..";
            }
            socket.onclose = function (ev) {
                let rt = document.getElementById("responseText");
                rt.value = rt.value + "\n" + "连接关闭了";
            }
        } else {
            alert("当前浏览器不支持websocket");
        }
        function send(msg) {
            console.log("lllllllllll", socket.readyState==WebSocket.OPEN)
            if (!socket) {
                return;
            }
            if (socket.readyState == WebSocket.OPEN) {
                console.log("发送成功")
                socket.send(msg);
            } else {
                alert("连接没有开启")
            }
        }
    </script>
    <form onsubmit="return false">
        <textarea name="message" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="发送消息" onclick="send(this.form.message.value)">
        <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="清空消息" onclick="document.getElementById('responseText').value = ''">
    </form>
</body>
</html>